import java.util.*;
import java.util.stream.Collectors;

public class StreamExample {
    public static void main(String[] args) {
        List<Product> products = Arrays.asList(
                new Product("Laptop", 1200.0, "Electronics"),
                new Product("Mouse", 25.0, "Electronics"),
                new Product("Keyboard", 75.0, "Electronics"),
                new Product("Chair", 150.0, "Furniture"),
                new Product("Table", 250.0, "Furniture")

        );
        System.out.println("所有产品信息：");
        products.stream()
                .forEach(System.out::println);

        long electronicsCount = products.stream()
                .filter(p -> "Electronics".equals(p.getCategory()))
                .count();
        System.out.println("电子产品数量：" + electronicsCount);

        Map<String, Double> productPriceMap = products.stream()
                .collect(Collectors.toMap(Product::getName,Product::getPrice));
        System.out.println("产品价格映射：\n" + productPriceMap);

        Optional<Double> total = products.stream()
                .map(Product::getPrice)
                .reduce(Double::sum);
        total.ifPresent(sum -> System.out.println("所有产品总价：" + sum));

//        List<Product> expensiveProducts = products.stream()
//                .filter(product -> product.getPrice() > 100.0)
//                .toList();
//        System.out.println("价格高于100的产品：\n" + expensiveProducts);
//
//        List<String> productNames = products.stream()
//                .map(product -> product.getName().toUpperCase())
//                .toList();
//        System.out.println("所有产品的大写名字：\n" + productNames);
//
//        List<Product> sortedByPrice = products.stream()
//                .sorted((p1 , p2) -> Double.compare(p1.getPrice(), p2.getPrice()))
//                .toList();
//        System.out.println("按价格排序后的产品：\n" + sortedByPrice);
//
//        List<String> sortedExpensiveProductNames = products.stream()
//                .filter(p -> p.getPrice() > 100.0)
//                .map(Product::getName)
//                .sorted()
//                .toList();
//        System.out.println("价格高于100且按名字排序的产品名称：\n" + sortedExpensiveProductNames);
    }
}
